package cn.wolfcode.wolf.article.service.impl;


import cn.wolfcode.wolf.article.domain.*;
import cn.wolfcode.wolf.article.mapper.StrategyContentMapper;
import cn.wolfcode.wolf.article.mapper.StrategyMapper;
import cn.wolfcode.wolf.article.service.DestinationService;
import cn.wolfcode.wolf.article.service.StrategyCatalogService;
import cn.wolfcode.wolf.article.service.StrategyService;
import cn.wolfcode.wolf.article.service.StrategyThemeService;
import cn.wolfcode.wolf.article.utils.OssUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy> implements StrategyService {
    private final StrategyCatalogService StrategyCatalogService;

    public StrategyServiceImpl(StrategyCatalogService StrategyCatalogService, DestinationService destinationService, StrategyThemeService strategyThemeService, StrategyContentMapper strategyContentMapper) {
        this.StrategyCatalogService = StrategyCatalogService;
        this.destinationService = destinationService;
        this.strategyThemeService = strategyThemeService;
        this.strategyContentMapper = strategyContentMapper;
    }

    private final DestinationService destinationService;

    private final StrategyThemeService strategyThemeService;
    private final StrategyContentMapper strategyContentMapper;

    @Override
    public Strategy getById(Serializable id) {
        Strategy strategy = super.getById(id);
        // 1. 获取攻略内容
        StrategyContent content = strategyContentMapper.selectById(id);
        strategy.setContent(content);
        return strategy;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(Strategy entity) {
        return dosaveOrUpdate(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(Strategy entity) {
        return dosaveOrUpdate(entity);
    }

    private boolean dosaveOrUpdate(Strategy entity) {
        // 1. 完成封面图片的上传, 且得到 url 后重新设置到 cover 属性中
        if (!StringUtils.isEmpty(entity.getCoverUrl()) || !entity.getCoverUrl().startsWith("https")) {
            String fileName = UUID.randomUUID().toString();
            String url = OssUtil.uploadImgByBase64("images/strategies", fileName + ".jpg", entity.getCoverUrl());
            entity.setCoverUrl(url);
        }
        // 2. 补充分类名称
        StrategyCatalog catalog = StrategyCatalogService.getById(entity.getCatalogId());
        entity.setCatalogName(catalog.getName());
        // 3. 根据分类中的目的地id/名称设置到攻略对象中
        entity.setDestId(catalog.getDestId());
        entity.setDestName(catalog.getDestName());
        // 4. 基于目的地判断是否是国外
        List<Destination> toasts = destinationService.findToasts(catalog.getDestId());
        Destination dest = toasts.get(0);
        if (dest.getId() == 1) {
            entity.setIsabroad(Strategy.ABROAD_NO);
        } else {
            entity.setIsabroad(Strategy.ABROAD_YES);
        }
        // 5. 查询主题, 设置主题名称
        StrategyTheme theme = strategyThemeService.getById(entity.getThemeId());
        entity.setThemeName(theme.getName());
        // 6. 设置创建时间
        if (entity.getId() == null) {
            entity.setCreateTime(new Date());
            // 7. 设置各种数量为0
            entity.setViewnum(0);
            entity.setSharenum(0);
            entity.setThumbsupnum(0);
            entity.setReplynum(0);
            entity.setFavornum(0);
            // 8. 重新设置默认状态, 覆盖前端提交的值
            entity.setState((Strategy.STATE_NORMAL));
            // 9. 保存攻略对象, 得到攻略自增的 id
            boolean save = super.save(entity);
            // 10. 将攻略 id 设置到内容对象中, 并保存内容对象
            StrategyContent content = new StrategyContent();
            content.setId(entity.getId());
            return save && strategyContentMapper.insert(content) > 0;
        }
        boolean ret = super.updateById(entity);
        StrategyContent content = entity.getContent();

        content.setId(entity.getId());
        return ret && strategyContentMapper.updateById(content) > 0;

    }

}
